package com.example.demo.lock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Syn  {

    public Lock lock = new ReentrantLock();

    public int[] nums = {1,2,3,4,5,6,7,8,9};

    public String[] str = {"A","B","C","D","E","F","G","H","I"};

    Condition condition1 = lock.newCondition();

    Condition condition2 = lock.newCondition();

    public void printfNum(){
        lock.lock();
        try{
            condition1.await();
            for (int i : nums){
                System.out.println(i);
                condition2.signal();
                condition1.await();
            }
            condition2.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void printStr(){
        lock.lock();
        try {
            for (String s:str){
                System.out.println(s);
                condition1.signal();
                condition2.await();
            }
            condition1.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }
}

class Test{

    public static void main(String[] args) {
        Syn s1 = new Syn();
        new Thread(s1::printfNum).start();
        new Thread(s1::printStr).start();
    }
}